<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/logo.png" alt="">
        <span>后台管理系统</span>
      </div>
      <el-button @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px':'200px'">
        <div class="toggle-button" @click="toggleCollapse">| | |</div>
        <el-menu :default-active="activePath" unique-opened :collapse-transition='false' router :collapse='isCollapse' background-color="#333744" text-color="#fff" active-text-color="#409EFF">
          <!-- 一级菜单 -->
          <el-submenu v-for="item in menuLists" :index="item.id + ''" :key="item.id">
            <template slot="title">
              <i :class="iconsList[item.id]"></i>
              <span slot="title">{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item v-for="subitem in item.children" :index="subitem.path+''" :key='subitem.id' @click="saveLinkActive(subitem.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span slot="title">{{subitem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { getMenuList } from '@/api/menu'
export default {
  name: 'Home',
  components: {},
  data () {
    return {
      menuLists: [],
      iconsList: {
        125: 'iconfont icon-user ',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  created () {
    this.menuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout () {
      window.sessionStorage.removeItem('token')
      this.$router.push('/login')
    },
    async menuList () {
      const { data: res } = await getMenuList()
      if (res.meta.status === 200) {
        this.menuLists = JSON.parse(JSON.stringify(res.data))
      }
    },
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    saveLinkActive (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
    }
  }
}
</script>
<style lang="less" scoped>
  .home-container{
    height: 100%;
  }
  .el-header {
    background-color: #373D41;
    color: #fff;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 20px;
    >div{
      display: flex;
      justify-content: center;
      align-items: center;
      >span{
        margin-left: 15px ;
      }
      img {
        width: 40px;
        height: 40px;
      }
    }
  }

  .el-aside {
    background-color: #333744;
    i {
        margin-right: 5px ;
    }
    .el-menu{
      border: none;
    }
    .toggle-button {
      background-color: #4A5064;
      font-size: 10px;
      line-height: 24px;
      color:#fff;
      text-align: center;
      cursor: pointer;
    }
  }

  .el-main {
    background-color: #E9EEF3;
  }

  body > .el-container {
    margin-bottom: 40px;
  }
</style>
